<template>
    <div class="execution flowContainer">
        <basic-container>
            <avue-crud
                ref="crud"
                :page.sync="page"
                :data="tableData"
                :permission="permissionList"
                :table-loading="tableLoading"
                :option="tableOption"
                @on-load="getList"
                @search-change="searchChange"
                @refresh-change="refreshChange"
                @size-change="sizeChange"
                @current-change="currentChange"
                @row-update="handleUpdate"
                @row-save="handleSave"
                @row-del="rowDel"
                @selection-change="selectionChange"
            >
                <template slot="menuLeft">
                    <el-button  type="primary" size="small"  @click="addjjr()">新增节假日</el-button>
                </template>
                <template slot="menuLeft">
                    <el-button  type="primary" size="small"  @click="batchDel()">批量删除</el-button>
                </template>
            </avue-crud>
            <history-routes ref="historyRoutes" style="display: none"></history-routes>
        </basic-container>
    </div>
</template>

<script>
    import {fetchList, getObj, addObj, putObj, delObj} from '@/views/wy-flow/api/sysholidayconf'
    import {tableOption} from '@/views/wy-flow/const/sysholidayconf'
    import {mapGetters} from 'vuex'
    import historyRoutes from '@/layout/history-routes'
    import dbl from '@/utils/dbl'
    import axios from 'axios'
    import Qs from 'qs'

    export default {
        name: 'sysholidayconf',
        components:{historyRoutes},
        data() {
            return {
                searchForm: {},
                tableData: [],
                selection:[],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.sysholidayconf_edit, false),
                    delBtn: this.vaildData(this.permissions.holidayDel, false),
                    editBtn: this.vaildData(this.permissions.sysholidayconf_edit, false)
                };
            }
        },
        methods: {
            addjjr(){
                // this.$refs.historyRoutes.delTab('/wy-flow/jiejiari')
                /*this.$router.push({
                    path:'/jiejiari2',//跳转路由
                });*/
                this.$router.push({name: 'holidays_add'})
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params, this.searchForm )).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            batchDel(){
                this.$confirm('是否确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.batchobj()
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            batchobj(){
                const loading = this.$loading({
                    lock: true,
                    text: '数据处理中'
                });
                var hids=[];
                for(var i=0;i<this.selection.length;i++){
                    hids.push(this.selection[i].hid);
                }
                axios.get("/api/holidayConf/batchDelete?hids="+hids).then(response => {
                    if(response.data.code===0){
                        this.$message.success('批量删除成功')
                    }
                    this.getList(this.page);
                    loading.close()
                }).catch(e => {
                    loading.close()
                })
            },
            selectionChange(row){
                this.selection=row;
            },
            rowDel: function (row, index) {
                this.$confirm('是否确认删除?' , '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.hid)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            handleSave: function (row, done,loading) {
                addObj(row).then(data => {
                    this.$message.success('添加成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            currentChange(current){
                this.page.currentPage = current
            },
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            refreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>

<style lang="scss" scoped>
    .flowContainer{
        /deep/ .avue-crud{
            padding-top: 30px;
            .avue-form__group .el-col:first-child{
                .el-form-item__label{
                    text-align: left;
                    width: 60px !important;
                }
                .el-form-item__content{
                    margin-left: 60px!important;
                }
            }
        }
    }
</style>
